package com.xdl.flytest.controller;

import com.xdl.flytest.bean.Report;
import com.xdl.flytest.service.ReportService;
import com.xdl.flytest.util.JsonResponseMessage;
import com.xdl.flytest.util.ResponseMsg;
import jdk.nashorn.internal.runtime.regexp.joni.Regex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * @Auther: 80736
 * @Date: 2019/1/28 12:51
 * @Description:
 */

@Controller
@RequestMapping("/test/")
public class ReportController {

    @Autowired
    private ReportService reportService;

    @GetMapping("/reports")
    public ModelAndView getAllReports() {

        List<Report> reports = reportService.getReports();
        ModelAndView mav = new ModelAndView();
        mav.setViewName("test/page-report");
        mav.addObject("reports", reports);
        return mav;
    }

    @GetMapping("/report")
    public ModelAndView viewReport(@RequestParam("id") Integer id) {
        Report report = reportService.getReportById(id);
        ModelAndView mav = new ModelAndView("test/page-report-templete.html");
        mav.addObject("report", report);
        return mav;
    }


    @DeleteMapping("/report")
    @ResponseBody
    public Map<String, Object> removeReport(@RequestParam("id") Integer id) {
        if (id == null) {
            return new JsonResponseMessage(ResponseMsg.PARAMETER_ERROR).returned();
        }
        reportService.removeReport(id);
        return new JsonResponseMessage(ResponseMsg.SUCCESS).returned();
    }


}
